<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Analy;
use App\Models\Counts;
use App\Models\CountsLog;
use App\Models\FormIds;
use App\Models\Records;
use App\Models\TmplmsgsLog;
use App\Models\Videos;
use App\Models\VideosCollect;
use App\Models\VideosCount;
use App\Models\VideosZan;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class AnalyController extends Controller{

    protected function index(){
        //小程序9.17开始运营
//        DB::transaction(function (){
//            $days = cal_days_in_month(CAL_GREGORIAN, 11, 2018);
//            for($d=1;$d<=12; $d++){
//                $app = app('wechat.mini_program');
//                if($d<10){
//                    $list = $app->data_cube->dailyVisitTrend('2018110'.$d, '2018110'.$d);
//                }else{
//                    $list = $app->data_cube->dailyVisitTrend('201811'.$d, '201811'.$d);
//                }
//                Analy::create([
//                    'created_at' => Carbon::createFromDate(2018, 11, $d),
//                    'visit_uv_new' => $list['list'][0]['visit_uv_new'],
//                    'visit_pv' => $list['list'][0]['visit_pv'],
//                    'visit_uv' => $list['list'][0]['visit_uv'],
//                ]);
//            }
//        });
        $params = [
            'start' => request('start',Carbon::now()->subDays(31)->toDateString()),
            'end' => request('end',Carbon::now()->subDays(1)->toDateString()),
        ];
        $lists = Analy::whereDate('created_at','>=' ,$params['start'])
            ->whereDate('created_at','<=' ,$params['end'])
            ->get();
        return $this->returnJson(0,'',$lists);
    }

    /**
     * FormID统计
     * @return \Illuminate\Http\JsonResponse
     */
    protected function formId(){
        $total = FormIds::count();
        $usable = FormIds::where('expires_at','>' ,Carbon::now())->distinct('openid')->count('openid');
        $added = FormIds::whereDate('created_at',Carbon::now()->toDateString())->count();
        $list = [
            ['total' => $total, 'usable' => $usable, 'added' => $added],
        ];
        $log = TmplmsgsLog::orderBy('created_at','DESC')->take(10)->get();
        $lists['list'] = $list;
        $lists['log'] = $log;
        return $this->returnJson(0,'',$lists);
    }

    /**
     * 视频统计
     * @return \Illuminate\Http\JsonResponse
     */
    protected function videosCount(){
        $date = [
            'start' =>(string)request('start', Carbon::now()->subDays(31)->toDateString()),
            'end' =>(string)request('end',  Carbon::now()->toDateString()),
            'uid' =>(int)request('id',auth()->guard('api')->id()),
        ];

        $lists = VideosCount::where(function ($query) use ($date){
                if($date['uid'] > 1){
                    $query->where('admin_id',$date['uid']);
                }
                if ($date['start']){
                    $query->whereDate('created_at','>=',$date['start']);
                }
                if ($date['end']){
                    $query->whereDate('created_at','<=',$date['end']);
                }
            })
            ->select(\DB::raw('sum(views) as views, sum(share_in) as share_in, sum(share_out) as share_out,date'))
            ->groupBy('date')
            ->orderBy('date','ASC')
            ->get();
        return $this->returnJson(0,'',$lists);
    }
}
